#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
//#include <rand.h>
#include <time.h>

static int initCounts = 0;
static bool isOver = false;

#define bt_log printf

static int get_random(void)
{
		srand(time(0));
		return rand();

		//return 0;
}

static void bt_jump_counts_timer(void)
{ 
    static int counts = 0;
    static int timeSec = 0;
    if(counts == 0)counts = initCounts;

    
    if(isOver)
    {
        isOver = false;
        counts = 0;
    }

    if(counts <= 0)
    {
        isOver = true;
    }
    else 
    {
        counts-=(get_random()%10 + 1);
        timeSec++;
    }
}

static void bt_jump_time_timer(void)
{
    static int counts = 0;
    static int timeSec = 0;
    if(timeSec == 0)timeSec = initCounts;

    if(isOver)
    {
        isOver = false;
        counts = 0;
    }

    if(timeSec == 0)
    {
        isOver = true;
    }
    else
    {
        timeSec--;
        counts+=(get_random()%10 + 1);
    }
}

static void bt_jump_free_timer(void)
{
    static int counts = 0;
    static int timeSec = 0;
    if(timeSec == 0)timeSec = initCounts;

    if(isOver)
    {
        isOver = false;
        counts = 0;
    }

    if(timeSec == 0)
    {
        isOver = true;
    }
    else
    {
        timeSec++;
        counts+=(get_random()%10 + 1);
    }
}

void bt_jump_timer_start(int cmd, int counts)
{
    initCounts = counts;
//    static timer_hnd_t timer_id = -1;

//    if(counts == 0)
//    {
//        ns_timer_cancel(timer_id);
//        isOver = true;
//    }
//    else
//    {
//        bt_log("cmd = %x\r\n", cmd);
//        switch(cmd)
//        {
//            case 0x80:
//                timer_id = ns_timer_create(1000, bt_jump_free_timer);
//                break;
//            case 0x81://计数
//                timer_id = ns_timer_create(1000, bt_jump_counts_timer);
//                break;
//            case 0x82://计时
//                timer_id = ns_timer_create(1000, bt_jump_time_timer);
//                break;
//            default:
//                break;
//        }
//    }
}

void tw_btjump_stop(void)
{
    bt_jump_timer_start(0, 0);
}

void bt_jump_recv_command(char *command, int len)
{
    char cmd_str[3];
    char counts_str[5];
    uint16_t counts;
    uint8_t cmd;

    /*
        自由跳绳：020380000B5D
        停止自由跳绳：0203000000845d
        倒计时跳绳：020382002375BC
        停止倒计时跳绳：020302000044FC
        倒计数跳绳：0203810023754C
        停止倒计数跳绳：0203010000440C
    */
    bt_log("command = %s", command);

    memset(cmd_str, 0, sizeof(cmd_str));
    memset(counts_str, 0, sizeof(counts_str));

    cmd_str[0] = command[4];
    cmd_str[1] = command[5];

    cmd = atoi(cmd_str);
    switch (cmd)
    {
			case 0x80:
			case 0x81:
			case 0x82:
        counts_str[0] = command[6];
        counts_str[1] = command[7];
        counts_str[2] = command[8];
        counts_str[3] = command[9];

        counts = atoi(counts_str);

        bt_jump_timer_start(cmd, counts);
        break;
        

			case 0x00:
			case 0x01:
			case 0x02:
        bt_jump_timer_start(cmd, 0);
        break;
    
    default:
        break;
    }
}
